package com.party.web.web.controller.merchantAnalyze;

import com.google.common.collect.Maps;
import com.party.core.model.commune.CDataAnalysisCountOutput;
import com.party.core.model.commune.CDateAnalysisIndexViewOutput;
import com.party.core.model.commune.IndexCQueryType;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.member.MemberMerchantDetail;
import com.party.core.model.system.RoleEnum;
import com.party.core.service.commune.ICDataAnalysisCountService;
import com.party.core.service.commune.ICDataAnalysisDetailService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IMemberMerchantDetailService;
import com.party.core.service.system.ISysRoleService;
import com.party.web.biz.merchantAnalyze.MemberMerchantDetailBizService;
import com.party.web.biz.merchantAnalyze.MerchantAnalyzeBizService;
import com.party.web.biz.order.OrderBizService;
import com.party.web.utils.RealmUtils;
import com.party.web.web.dto.AjaxResult;
import com.party.web.web.dto.output.merchantAnalyze.MerchantAnalyzeOutput;
import com.party.web.web.security.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 机构数据分析
 * Created by wei.li
 *
 * @date 2017/9/14 0014
 * @time 16:56
 */

@Controller
@RequestMapping(value = "merchant/analyze")
public class MerchantAnalyzeController {

    @Autowired
    private MerchantAnalyzeBizService merchantAnalyzeBizService;

    @Autowired
    private OrderBizService orderBizService;

    @Autowired
    private MemberMerchantDetailBizService memberMerchantDetailBizService;

    @Autowired
    private IMemberMerchantDetailService memberMerchantDetailService;

    @Autowired
    private IMemberGroupService memberGroupService;
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ICDataAnalysisDetailService cDataAnalysisDetailService;
    @Autowired
    private ICDataAnalysisCountService cDataAnalysisCountService;

    /**
     * 数据分析
     *
     * @return 数据分析
     */
    @RequestMapping(value = "detail")
    public ModelAndView detail() {
        CurrentUser member = RealmUtils.getNewCurrentUser();
        boolean isXzgs = sysRoleService.hasRole(member.getId(), RoleEnum.KNOWING_COMMUNE_ROLE.getCode());
        ModelAndView modelAndView = new ModelAndView();
        if (isXzgs) {
            modelAndView.setViewName("merchantAnalyze/communityAnalyzeDetail");
            String memberGroupId = member.getId();
            if (member.isTwoLevelChildren()) {
                memberGroupId = member.getPartnerId();
            }
            HashMap<String, CDateAnalysisIndexViewOutput> map = memberMerchantDetailBizService.cdataAnalyzeDetail(memberGroupId);
            modelAndView.addObject("map", map);
        } else {
            modelAndView.setViewName("merchantAnalyze/merchantAnalyzeDetail");
            HashMap<String, MerchantAnalyzeOutput> map = memberMerchantDetailBizService.detail(member.getId());
            modelAndView.addObject("map", map);
        }

        memberIndexCommon(modelAndView, null);
        boolean isChild = member.isChild();
        if (member.isChild()) {
            MemberGroup partner = memberGroupService.getPartner(member.getId());
            modelAndView.addObject("partnerName", partner.getRealname());
        }
        modelAndView.addObject("isChildAccount", isChild);
        modelAndView.addObject("member", member);
        return modelAndView;
    }

    /**
     * 图表
     *
     * @param type   类型
     * @param length 长度
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "chart")
    public AjaxResult chart(Integer type, Integer length) {
        MemberGroup member = RealmUtils.getCurrentUser();
        AjaxResult ajaxResult = new AjaxResult();
        length = null == length ? 30 : length;
        type = null == type ? 3 : type;
        List<String> dateList = memberMerchantDetailBizService.dateStringList(length);
        //List<String> data = merchantAnalyzeBizService.chartList(length, member.getId(), type);
        List<String> data = memberMerchantDetailBizService.oneChartList(length, member.getId(), type);
        HashMap<String, List> map = Maps.newHashMap();
        map.put("dateList", dateList);
        map.put("dataList", data);
        ajaxResult.setData(map);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    /**
     * 图表
     *
     * @param type   类型
     * @param length 长度
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "community/chart")
    public AjaxResult communityChart(Integer type, Integer length) {
        MemberGroup member = RealmUtils.getCurrentUser();
        AjaxResult ajaxResult = new AjaxResult();
        //默认查询条件
        length = null == length ? 30 : length;
        type = null == type ? 3 : type;

        String meberGroupId = member.getId();
        if(RealmUtils.getNewCurrentUser().isTwoLevelChildren()){
            meberGroupId = member.getPartnerId();
        }
        List<String> memberGroupIds = new ArrayList<>();
        memberGroupIds.add(meberGroupId);
        List<String> dateList = memberMerchantDetailBizService.dateStringList(length);
        List<String> data = memberMerchantDetailBizService.communityOneChartList(length, memberGroupIds, type);
        HashMap<String, List> map = Maps.newHashMap();
        map.put("dateList", dateList);
        map.put("dataList", data);
        ajaxResult.setData(map);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    /**
     * 动态tab
     *
     * @param cType 类型
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "community/indexTab")
    public AjaxResult communityIndexTab(Integer cType) {
        MemberGroup member = RealmUtils.getCurrentUser();
        HashMap<String, Object> map = Maps.newHashMap();
        try {
            //获取查询条件
            String[] cQueryTypes = IndexCQueryType.getValues(cType);
            map.put("cQueryTypes", cQueryTypes);

            //获取条件下对应的值
            HashMap<String, Object> params = Maps.newHashMap();
            List<String> memberGroupIds = memberGroupService.getChildWithMy(member.getId());
            params.put("memberGroupIds",memberGroupIds);
            List<CDataAnalysisCountOutput> dataList = cDataAnalysisCountService.sumOrgList(params);
            map.put("dataList", dataList);
        } catch (Exception e) {
            return AjaxResult.error("动态tab查询异常");
        }
        return AjaxResult.success(map);
    }

    public void memberIndexCommon(ModelAndView mv, Integer type) {
        Map<String, Object> indexMap = orderBizService.memberIndexMap(true);
        mv.addAllObjects(indexMap);
    }

    /**
     * 初始化viewNum数据
     *
     * @return
     * @date 2018/3/8
     */
    @ResponseBody
    @RequestMapping(value = "initDetailViewNum")
    public AjaxResult updateViewNum() {
        //查询viewNum*10+x->viewNum
        //List<MemberMerchantDetail> viewNumList = memberMerchantDetailService.getAllViewNum();
        //更新循环viewNum
       /*for (MemberMerchantDetail  md: viewNumList) {
            memberMerchantDetailService.batchUpdate(md);
        }*/
        return AjaxResult.success();
    }

    /**
     * 处理还原后浏览量+1的业务
     *
     * @return
     * @date 2018/3/13
     */
    @ResponseBody
    @RequestMapping(value = "initViewNum")
    public AjaxResult updateHistoryViewNum() {
        String createStart = "2018-03-10";
        List<MemberMerchantDetail> viewNumList = memberMerchantDetailService.getAllViewNum(createStart);
        for (MemberMerchantDetail md : viewNumList) {
            memberMerchantDetailService.batchUpdate(md);
        }
        return AjaxResult.success();
    }

}
